package com.tenbit.tbw.ctl;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.tenbit.tbw.svc.EMP1010SVC;
import com.tenbit.tbw.svc.PDT1020SVC;
import com.tenbit.tbw.vo.EMP1020VO;
import com.tenbit.tbw.vo.PDT1010VO;

@Controller
public class PDT1020CTL implements ApplicationContextAware {
	
	private WebApplicationContext context = null;
	
	@Autowired
	private PDT1020SVC svc;
	
	@Autowired
	private EMP1010SVC svc1;
	
	private Logger log=Logger.getLogger(this.getClass());
	private boolean result=false;
	private Map<String,Object> resultMap = new HashMap<String,Object>();
	private String msg=null;
	
//	@RequestMapping("PDT1020P1.do")
	public ModelAndView getProductInfoDetail(PDT1010VO vo,HttpServletRequest request)
	{
		log.debug(" ==============PDT1010P1.do-getProductInfoDetail: 상품상세정보 목록 ==============");
		ModelAndView mav = new ModelAndView();
		String mode=null;
		if(vo.getProductNumber()==0){
			mode="I";
			mav.addObject("mode",mode);
			mav.addObject("model", svc1.getEmployeeCodebook());
			mav.setViewName("2nd_week/PDT1020P1");
			return mav;
		} 
		
		resultMap=svc.getProductInfoDetail(vo);
		
		result = (Boolean) resultMap.get("result");
		if(result){
			msg=(String) resultMap.get("msg");
		} else {
			msg=(String) resultMap.get("resultMassg");
			mav.addObject("msg",msg);
		}
		PDT1010VO productDetailInfo=(PDT1010VO) resultMap.get("productDetailInfo");
		log.debug(productDetailInfo);
		
		mode="M";
		mav.addObject("mode",mode);
		mav.addObject("result",result);
		mav.addObject("productDetailInfo",productDetailInfo);
		mav.addObject("model", svc1.getEmployeeCodebook());
		mav.setViewName("2nd_week/PDT1020P1");
		return mav;
	}
	
//	@RequestMapping("PDT1020P2.do")
	public ModelAndView getEmployeeInfo(HttpServletRequest request,HttpServletResponse Response)
	{
		ModelAndView mav = new ModelAndView();
		
		String name=request.getParameter("employeeName");


		int page=1;
		int limit=10;
		if(request.getParameter("page") != null)
		{
			page = Integer.parseInt(request.getParameter("page"));
		}
		
		int listcount=0;

		listcount=(Integer) svc1.getSearchEmpNum(name);
	
		
		List<EMP1020VO> list = null;
	
		list=svc1.getSearchList(page,limit,name);
		
		int maxpage = (int)((double)listcount/limit+0.95);
		int startpage =(((int)((double)page / 10+0.9))-1)*10+1;
	
		mav.addObject("searchList",list );
		mav.addObject("name",name);
		mav.addObject("page", page);
		mav.addObject("startpage", startpage);
		mav.addObject("maxpage", maxpage);
		mav.setViewName("2nd_week/PDT1020P2");
		return mav;
	}
	
//	@RequestMapping("PDT1020P11.do")
	@ResponseBody
	public String getCategoryName(HttpServletRequest request){
		log.debug("AJAX시작");
		String categoryName=null;
		
		String itemCode=request.getParameter("itemCode");
		
		categoryName=svc.getCategoryName(itemCode);
		
		return categoryName;
	}
//	@RequestMapping("PDT1020P12.do")
	public ModelAndView getProductInfoInput(PDT1010VO vo)
	{
		log.debug(" ==============PDT1010P12.do-getProductInfoInput: 신규등록 ==============");
		ModelAndView mav = new ModelAndView();
		String mode=null;
		if(vo.getProductNumber()==0){
			mode="I";
			mav.addObject("mode",mode);
			mav.addObject("model", svc1.getEmployeeCodebook());
			mav.setViewName("2nd_week/PDT1020P1");
			return mav;
		}
		
		resultMap=svc.getProductInfoDetail(vo);
		
		result = (Boolean) resultMap.get("result");
		if(result){
			msg=(String) resultMap.get("resultMassg");
		} else {
			msg=(String) resultMap.get("msg");
			mav.addObject("msg",msg);
		}
		PDT1010VO productDetailInfo=(PDT1010VO) resultMap.get("productDetailInfo");
		log.debug(productDetailInfo);
		
		mode="V";
		mav.addObject("mode",mode);
		mav.addObject("result",result);
		mav.addObject("productDetailInfo",productDetailInfo);
		mav.addObject("model", svc1.getEmployeeCodebook());
		mav.setViewName("2nd_week/PDT1020P1");
		return mav;
	}
//	@RequestMapping("PDT1020P13.do")
	public ModelAndView delProductInfo(PDT1010VO vo)
	{
		log.debug(" ==============PDT1010P13.do-deleteProductInfoInput:삭제 ==============");
		ModelAndView mav = new ModelAndView();
		String mode=null;
		
		resultMap=svc.delProductInfoDetail(vo);
		
		result = (Boolean) resultMap.get("result");
		if(result){
			msg=(String) resultMap.get("msg");
			mode="V";
		} else {
			msg=(String) resultMap.get("resultMassg");
			mode="M";
		}
		mav.addObject("msg",msg);
		mav.addObject("mode",mode);
		mav.addObject("result",result);
		mav.addObject("productDetailInfo",vo);
		mav.addObject("model", svc1.getEmployeeCodebook());
		mav.setViewName("2nd_week/PDT1020P1");
		return mav;
	}
//	@RequestMapping("PDT1020P14.do")
	public ModelAndView setProductDetailinsert(PDT1010VO vo)
	{
		log.debug(" ==============PDT1010P13.do-deleteProductInfoInput:등록/수정 ==============");
		ModelAndView mav = new ModelAndView();
		
		MultipartFile attachedFile = vo.getAttachedFile();
		log.debug(attachedFile);
		
		try{
		if (attachedFile != null) {
            String fileName = attachedFile.getOriginalFilename();
            if(fileName!=null && !fileName.equals("")){
            vo.setAttachedFileName(fileName);
            	File file = new File("C:/TBWORKSHOP/workspace/tbworkshop/WebContent/WEB-INF/views/file/" + fileName);
            	attachedFile.transferTo(file);
            }
		}
		}	catch (IOException e) {
			e.printStackTrace();
		}
		
		String mode=null;
		log.debug(vo);
		
		if(vo.getProductNumber()!=0){
			resultMap=svc.setProductInfoDetail(vo);
			result = (Boolean) resultMap.get("result");
			if(result){
				msg=(String) resultMap.get("msg");
				mode="V";
			} else {
				msg=(String) resultMap.get("msg");
				mode="I";
			}
		}else{
			resultMap=svc.insProductInfoDetail(vo);
			result = (Boolean) resultMap.get("result");
			if(result){
				msg=(String) resultMap.get("msg");
				mode="V";
			} else  {
				msg=(String) resultMap.get("msg");
				mode="I";
			}
			
		}
		result = (Boolean) resultMap.get("result");
			
		mav.addObject("msg",msg);
		mav.addObject("mode",mode);
		mav.addObject("result",result);
		mav.addObject("productDetailInfo",vo);
		mav.addObject("model", svc1.getEmployeeCodebook());
		mav.setViewName("2nd_week/PDT1020P1");
		return mav;
	}
	
//	 @RequestMapping("download.do")
	    public ModelAndView download(@RequestParam("attachedFileName") String fileName) {
		 
		 
		 String fullPath = "C:/TBWORKSHOP/workspace/tbworkshop/WebContent/WEB-INF/views/file/"+ fileName ;

	         
	        File file = new File(fullPath);
	         
	        return new ModelAndView("download", "downloadFile", file);
	    }
	 
	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		// TODO Auto-generated method stub
		this.context = (WebApplicationContext)arg0;
	}
	
	
}
